/**
 * Copyright (c) 2019-2029, wwww.kiwipeach.cn (liuburu@qq.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * https://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package cn.kiwipeach.blog.controller.demo;

import cn.kiwipeach.blog.base.AjaxResponse;
import cn.kiwipeach.blog.generator.domain.Blog;
import cn.kiwipeach.blog.service.demo.IcacheDemoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.PostConstruct;
import java.util.Collection;

/**
 * 描述：ehcache缓存案例测试
 *
 * @author kiwipeach
 * @since 1.0
 */
//@Api(tags = "测试模块（缓存测试）")
@Controller
@RequestMapping("/demo/cache")
public class CacheDemoController {

    @Autowired
    private IcacheDemoService icacheDemoService;

    /**
     * 实现类有：
     * class org.springframework.data.redis.cache.RedisCacheManager
     * class org.springframework.cache.ehcache.EhCacheCacheManager
     */
    @Autowired
    org.springframework.cache.CacheManager cacheManager;

    @PostConstruct
    public void init() {
    }

    @ApiOperation(value = "查看缓存信息")
    @GetMapping("cache/{cacheName}/{key}")
    @ResponseBody
    public AjaxResponse ladeCache(@PathVariable("cacheName") String cacheName,@PathVariable("key") String key) {
        // 重置缓存
        Cache cache = cacheManager.getCache(cacheName);
        Cache.ValueWrapper valueWrapper = cache.get(key);
        return (AjaxResponse) valueWrapper.get();
    }

    @ApiOperation(value = "缓存清空")
    @GetMapping("clear")
    @ResponseBody
    public AjaxResponse<String> reloadCache() {
        Collection<String> cacheNames = cacheManager.getCacheNames();
        // 重置缓存
        for (String cacheName : cacheNames) {
            Cache cache = cacheManager.getCache(cacheName);
            cache.clear();
        }
        return AjaxResponse.success("重置缓存成功");
    }

    @ApiOperation(value = "查询博客，同时进行缓存")
    @GetMapping("query/{blogId}")
    @ResponseBody
    public AjaxResponse<Blog> queryBlogById(@PathVariable("blogId") String blogId) {
        return icacheDemoService.selectById(blogId);
    }

    @ApiOperation(value = "新建博客，同时缓存进行缓存")
    @GetMapping("create")
    @ResponseBody
    public AjaxResponse<Blog> createBlog() {
        Blog blog = new Blog();
        blog.setId("demoId001");
        blog.setTitle("demo title");
        blog.setContent("demo content");
        blog.setUserId("kiwipeach");
        return icacheDemoService.save(blog);
    }

    @ApiOperation(value = "更新博客，同时更新缓存")
    @GetMapping("update")
    @ResponseBody
    public AjaxResponse<Blog> updateBlog() {
        Blog blog = new Blog();
        blog.setId("demoId001");
        blog.setTitle("updated title");
        return icacheDemoService.updateById(blog);
    }

    @ApiOperation(value = "博客删除，同时清空缓存")
    @GetMapping("delete/{blogId}")
    @ResponseBody
    public AjaxResponse<Boolean> deleteBlogById() {
        String blogId = "demoId001";
        return icacheDemoService.deleteById(blogId);
    }

}
